package com.csgc.camera.widget.dialog

import android.content.Context
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.csgc.camera.biz.other.activity.AddCustomMaskActivity
import com.csgc.camera.constants.MaskConfig
import com.csgc.camera.events.ChangeCustomMaskStatusEvent
import com.csgc.camera.events.CustomMaskEvent
import com.csgc.camera.events.MaskDataStyleEvent
import com.csgc.camera.events.MaskEditDateEvent
import com.csgc.camera.events.MaskLocationEvent
import com.csgc.camera.events.MaskUpdateContent
import com.csgc.camera.util.TimeUtil
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.widget.dialog.adapter.MaskEditInfoAdapter
import com.csgc.camera.widget.dialog.base.BottomDialog
import com.cysd.spsyxj.R
import com.drake.channel.receiveEventHandler
import com.drake.channel.sendEvent
import kotlinx.coroutines.Job

class MaskEditDialog(val mContext: Context) {

    private lateinit var mDialog: BottomDialog
    private var mRecycleEditContent:RecyclerView?=null
    private val adapter: MaskEditInfoAdapter by lazy {
       val adapter =  MaskEditInfoAdapter(mContext)
        adapter.maskListener = object :MaskEditInfoAdapter.OnMaskItemListener{
            override fun onStatusChange(position: Int, status: Boolean, isCustom: Boolean) {
                if(isCustom){
                    currentMask?.changeCustomStatus()
                    sendEvent(ChangeCustomMaskStatusEvent(status))
                }
            }
        }
        adapter
    }
    private var maskContainer:LinearLayout?=null
    private var receiver: Job?=null
    private var receiverEditDateStyle: Job?=null
    private var receiverEditDate: Job?=null
    private var receiverUpdateContent: Job?=null
    private var receiverUpdateLocation: Job?=null

    init {
        initDialog()
    }


    private fun initDialog(){

        mDialog = BottomDialog(mContext).apply {
            setCustomView(R.layout.dialog_mask_edit)
            findViewById<View>(R.id.iv_close)?.clickWithTrigger { dismiss() }
            findViewById<View>(R.id.tv_sure)?.clickWithTrigger { dismiss() }
            findViewById<View>(R.id.layout_add_custom)?.clickWithTrigger {
                AddCustomMaskActivity.launch(mContext)
            }
            maskContainer = findViewById(R.id.layout_mask_container)
            mRecycleEditContent = findViewById(R.id.rv_edit_content)
            val manager = LinearLayoutManager (mContext,GridLayoutManager.VERTICAL,false)
            manager.orientation = GridLayoutManager.VERTICAL
            mRecycleEditContent?.layoutManager = manager
            mRecycleEditContent?.adapter = adapter
//            setCancelable(false)

            setOnDismissListener {
                receiver?.cancel()
                receiverEditDateStyle?.cancel()
                receiverEditDate?.cancel()
                receiverUpdateContent?.cancel()
            }
        }

    }



    fun initData(type:Int,id:Int){

        MaskConfig.updateData(type,id)
        adapter.update(MaskConfig.maskMap[type]!!,MaskConfig.customMaskInfo)

    }

    fun refreshFilterView(){
        adapter?.notifyDataSetChanged()
    }

    private var currentType = -1
    private var currentMaskId = -1
    private var currentMask:CustomMaskHandle?=null
    fun show(type: Int,maskId:Int){
        currentType = type
        currentMaskId = maskId
        maskContainer?.removeAllViews()
        currentMask = null

        val maskView = MaskConfig.getMaskView(mContext, currentType,currentMaskId)
        currentMask = maskView
        maskContainer?.addView(maskView)

        mDialog.show()

        initEvents()

        }




    private fun initEvents(){
        //添加新的水印信息
        receiver =  receiveEventHandler<CustomMaskEvent> {
            adapter.add(MaskConfig.customMaskInfo[MaskConfig.customMaskInfo.size-1])
            currentMask?.onAddCustom()
        }

        //更新时间格式
        receiverEditDateStyle = receiveEventHandler<MaskDataStyleEvent> {event->
            MaskConfig.maskMap.forEach {
                it.value.forEach {info->
                    if (info.title == "拍摄时间" && !info.isCustom){
                        info.content = TimeUtil.getTimeFormat(System.currentTimeMillis(),event.pattern)
                    }
                }
            }
            adapter.notifyDataSetChanged()
        }
        //修改时间
        receiverEditDate = receiveEventHandler<MaskEditDateEvent> { event->
            MaskConfig.maskMap.forEach {
                it.value.forEach {info->
                    if (info.title == "拍摄时间" && !info.isCustom){
                        var style = MaskConfig.maskDateStyle
                        if (style.isEmpty()){
                            style = MaskConfig.getDefaultDateStyle(currentType,currentMaskId)
                        }
                        info.content = TimeUtil.getTimeFormat(event.date,style)

                    }else if (info.title == "修改时间" && !info.isCustom){
                        info.content = TimeUtil.getTimeFormat(event.date,"yyyy-MM-dd HH:mm")
                    }
                }
            }
            adapter.notifyDataSetChanged()
        }

        receiverUpdateContent =  receiveEventHandler<MaskUpdateContent> {
            adapter.notifyDataSetChanged()
        }

        //位置修改
        receiverUpdateLocation =  receiveEventHandler<MaskLocationEvent> {
            MaskConfig.maskMap.forEach {
                it.value.forEach {info->
                    if (info.title == "地点" && !info.isCustom){
                        info.content = MaskConfig.maskLocation
                    }
                }
            }
            adapter.notifyDataSetChanged()
        }
    }


}